package autohome.wz.job;

import autohome.wz.lib.AutoHomeNewsHelper;
import autohome.wz.lib.Common;
import autohome.wz.lib.HttpRequest;
import autohome.wz.model.AutoHomeNews;
import autohome.wz.model.ShuoKe.ShuoKeApiResult;
import autohome.wz.model.ShuoKe.ShuoKeArticle;
import autohome.wz.model.ShuoKe.ShuoKeDetail;
import autohome.wz.model.ShuoKe.ShuoKeResult;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;

import java.util.Date;
import java.util.List;

/**
 * Created with Administrator
 * DATE:2016/12/22
 * Time:17:28
 */
public class ShuoKeJob implements Job {


    //说客：http://cms.api.autohome.com.cn/baseapi/blog/BlogArticleList.ashx?_appid=app&pagesize=10&pageindex=4
    //说客详情页：http://cms.api.autohome.com.cn/baseapi/blog/BlogArticleDetail.ashx?id=9858&_appid=m


    private static int currentPage = 1;


    private static Logger logger = Logger.getLogger(ShuoKeJob.class);

    public void execute(JobExecutionContext arg0) {
        GetData();
    }


    public static void main(String[] arg0) {
        GetData();
    }


    private static void GetData() {

        int bizType = 2;
        int TotalPageCount = 600;
        String url = "http://cms.api.autohome.com.cn/baseapi/blog/BlogArticleList.ashx";
        String result = HttpRequest.sendGet(url, "_appid=app&pagesize=10&pageindex=" + currentPage);
        if (!result.equals("")) {
            Gson gson = new Gson();
            ShuoKeApiResult<ShuoKeResult> apiResult;
            try {
                apiResult = gson.fromJson(result, new TypeToken<ShuoKeApiResult<ShuoKeResult>>() {
                }.getType());
                if (apiResult != null) {
                    List<ShuoKeArticle> list = apiResult.getResult().getArticleList();
                    if (list != null && list.size() > 0) {
                        for (ShuoKeArticle article : list) {

                            long articleId = article.getId();
                            if (articleId > 0) {
                                ShuoKeApiResult<ShuoKeDetail> articleDetail = GetShuoKeDetail(articleId);
                                if (articleDetail != null) {

                                    String strTitle = article.getTitle();
                                    String strDate = article.getTime();
                                    int day = Common.differenceDay(strDate);
                                    if (day <= 30) {
                                        String strPicUrl = Common.getAutoHomeImageSrc(article.getSmallPic());
                                        String strAuthor = article.getNickName();
                                        String strSeriesIds = article.getSeriesId() == null ? "" : article.getSeriesId();
                                        String strSummary = article.getSummary();
                                        if (strSeriesIds != null && strSeriesIds.length() > 0) {
                                            if (strSeriesIds.split(",").length > 1) {
                                                strSeriesIds = strSeriesIds.split(",")[0];
                                            }
                                        }
                                        String strTags = article.getTags() == null ? "" : article.getTags();
                                        String strContent = articleDetail.getResult().getContent();
                                        AutoHomeNews model = new AutoHomeNews();
                                        model.setAuthor(strAuthor);
                                        model.setTitle(strTitle);
                                        model.setBiz_id(articleId);
                                        model.setBiz_type(bizType);
                                        model.setCms_content_class("");
                                        model.setCms_series_id(strSeriesIds);
                                        model.setCms_tags(strTags);
                                        model.setContent(strContent);
                                        model.setImg_url(strPicUrl);
                                        model.setGraphic_img_list2("");
                                        model.setIndex_detail(strSummary);
                                        model.setImg_url2("");
                                        model.setPublish_time(Common.GetSqlTimeSpan(strDate));
                                        int addResult = AutoHomeNewsHelper.Add(model);
                                        if (addResult > 0) {
                                            Common.showMessage(strTitle + "入库成功");
                                            logger.info("说客入库成功 ");
                                        }
                                    }

                                }
                            }
                        }
                        if (currentPage < TotalPageCount) {
                            currentPage++;
                        }
                        GetData();


                    }

                }
            } catch (Exception e) {
                e.printStackTrace();
                Common.showMessage(e.getMessage());
            }
        }
    }


    /**
     * 获取说客文章详情内容
     *
     * @param ShuoKeId 文章ID
     * @return 说客文章内容详情
     */
    private static ShuoKeApiResult<ShuoKeDetail> GetShuoKeDetail(long ShuoKeId) {
        String baseDetailUrl = "http://cms.api.autohome.com.cn/baseapi/blog/BlogArticleDetail.ashx";
        ShuoKeApiResult<ShuoKeDetail> result = new ShuoKeApiResult<ShuoKeDetail>();
        if (ShuoKeId > 0) {
            String apiResult = HttpRequest.sendGet(baseDetailUrl, "id=" + ShuoKeId + "&_appid=m");
            if (apiResult != "" && apiResult.length() > 0) {
                Gson gson = new Gson();
                try {
                    result = gson.fromJson(apiResult, new TypeToken<ShuoKeApiResult<ShuoKeDetail>>() {
                    }.getType());
                    if (result != null) {
                        return result;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    logger.error("GetShuoKeDetail:" + e.getMessage());
                }
            }
        }
        return result;
    }
}
